{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab7840eb-944c-4924-837f-bb8d4c3ac006",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install pip==23.0\n",
    "!pip3 install bitsandbytes==0.39.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23f1222b-9aa0-4436-8c77-873d056cb050",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-28T13:09:14.149488Z",
     "iopub.status.busy": "2025-04-28T13:09:14.149285Z",
     "iopub.status.idle": "2025-04-28T13:09:14.322253Z",
     "shell.execute_reply": "2025-04-28T13:09:14.321604Z",
     "shell.execute_reply.started": "2025-04-28T13:09:14.149471Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fatal: 目标路径 'LLaMA-Factory' 已经存在，并且不是一个空目录。\n"
     ]
    }
   ],
   "source": [
    "!git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d3140a18-989f-4c83-a549-8e6c5297fedf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-28T13:09:21.073456Z",
     "iopub.status.busy": "2025-04-28T13:09:21.073146Z",
     "iopub.status.idle": "2025-04-28T13:09:21.077253Z",
     "shell.execute_reply": "2025-04-28T13:09:21.076831Z",
     "shell.execute_reply.started": "2025-04-28T13:09:21.073438Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/workspace/LLaMA-Factory\n"
     ]
    }
   ],
   "source": [
    "%cd LLaMA-Factory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "176be470-faa2-4d18-8465-99c0f39335f6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install -e .[torch,bitsandbytes]"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 6,
   "source": [
    "import json\n",
    "\n",
    "#%cd /mnt/LLaMA-Factory/\n",
    "\n",
    "NAME = \"测试AI\"\n",
    "AUTHOR = \"AI工厂\"\n",
    "\n",
    "with open(\"data/identity.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "  dataset = json.load(f)\n",
    "\n",
    "for sample in dataset:\n",
    "  sample[\"output\"] = sample[\"output\"].replace(\"{{\"+ \"name\" + \"}}\", NAME).replace(\"{{\"+ \"author\" + \"}}\", AUTHOR)\n",
    "\n",
    "with open(\"data/identity.json\", \"w\", encoding=\"utf-8\") as f:\n",
    "  json.dump(dataset, f, indent=2, ensure_ascii=False)"
   ],
   "id": "543a8e10-c1ee-4c5d-8c78-4b247bbf37e4"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f44fd93-8e53-4150-adda-87ee06612230",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone https://www.modelscope.cn/Qwen/Qwen2.5-7B-Instruct.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc7b6a39-5cd3-4d16-ac58-8f7e898f1db0",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!llamafactory-cli train examples/train_lora/qwen_lora_sft.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5ed3e38-78c4-4390-a9b1-20860b109023",
   "metadata": {
    "ExecutionIndicator": {
     "show": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# terminal运行 ： llamafactory-cli chat examples/inference/qwen_lora_sft.yaml"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "# terminal运行 : llamafactory-cli export examples/merge_lora/qwen_lora_sft.yaml",
   "id": "39250ddba81af3f7"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
